import express from "express";

const router = express.Router();
router.get("/", (req, res) => {
  res.render("ws/index.njk",{username: '张三'});
});

// 获取所有的ws客户端
router.get("/clients", (req, res) => {
  res.json({
    code: 200,
    data: [],
  });
});

router.post("/reTask", (req, res) => {
  // 判断是否有任务，有任务就覆盖执行。 否则添加上去
  // 任务id也是组合id
  const { id, type } = req.body;

  const wss = getWss();
  // 群发
  if (!type && !id) {
    const all = setInterval(() => {
      wss.clients.forEach((item) => {
        if (item.readyState === WebSocket.OPEN) {
          item.send(message);
        }
      });
    }, 10000);
    // addTask("all", all);
  } else if (type) {
    const taskId = `${type}-${id}`;
    const all = setInterval(() => {
      wss.clients.forEach((item) => {
        if (item.readyState === WebSocket.OPEN) {
          item.send(message);
        }
      });
    }, 10000);
    // addTask("taskId", all);
  }

  // console.log(wsClients);

  // 先判断是否有任务，有任务就覆盖执行。 否则添加上去
  // 任务id也是组合id
  // const task = reTasks[taskId];
  // console.log(req.body);
  // addTask(req.body)
});
export default router;
